/**
 * Copyright � 2012 GestureSoft. ALL RIGHTS RESERVED.
 * This file contains proprietary and GestureSoft CONFIDENTIAL Information.
 * Use, disclosure or reproduction is prohibited.
 * 
 * @fileName DBEncryptor.as
 * @creationDate Aug 8, 2012
 * @author 
 * @version 1.0
 */
package com.gesturesoft.imultimax.model.dbmanager
{
	import com.hurlant.crypto.Crypto;
	import com.hurlant.crypto.hash.HMAC;
	import com.hurlant.crypto.hash.IHash;
	import com.hurlant.crypto.prng.Random;
	import com.hurlant.crypto.symmetric.ICipher;
	import com.hurlant.crypto.symmetric.IMode;
	import com.hurlant.crypto.symmetric.IPad;
	import com.hurlant.crypto.symmetric.IVMode;
	import com.hurlant.crypto.symmetric.NullPad;
	import com.hurlant.crypto.symmetric.PKCS5;
	import com.hurlant.util.Base64;
	import com.hurlant.util.Hex;
	
	import flash.utils.ByteArray;
	
	public class DBEncryptor
	{
		
		public function DBEncryptor()
		{
			
		}
		
		
		private static var currentInput:ByteArray;
		private static var currentResult:ByteArray;
		private static var key:String = '85e751e51716f0f805789fa9efcf8139d1738c3a81fe05c0938e2430ad88f2b0';
		
		private static function gen(v:int):String {
			
			//** Random key generator function not in use
			//keyFormat.selectedIndex=1;
			var r:Random = new Random;
			var b:ByteArray = new ByteArray
			r.nextBytes(b, v/8);
			return Hex.fromArray(b);
		}
		
		public static function encrypt(str:String):String {
			// 2: get a key
			var k:String = key;
			var kdata:ByteArray;
			var kformat:String = "text";
			
			kdata = Hex.toArray(Hex.fromString(k));
			
			
			// 3: get an input
			//============================INPUT TEXT====================
			var txt:String = str;
			var data:ByteArray;
			var format:String = "text";
			
			data = Hex.toArray(Hex.fromString(txt));
			
			// 1: get an algorithm..
			var name:String = "aes"+"-"+"ecb";
			
			
			var pad:IPad = new PKCS5;
			var mode:ICipher = Crypto.getCipher(name, kdata, pad);
			pad.setBlockSize(mode.getBlockSize());
			mode.encrypt(data);
			currentResult = data;
			var encryptReturn:String = displayOutput();
			// populate IV.
			if (mode is IVMode) {
				var ivmode:IVMode = mode as IVMode;
			}
			
			return encryptReturn;
		}
		
		public static function  decrypt(str:String):String {
			// 2: get a key
			var k:String = key;
			var kdata:ByteArray;
			var kformat:String = "text";
			switch (kformat) {
				case "hex": kdata = Hex.toArray(k); break;
				case "b64": kdata = Base64.decodeToByteArray(k); break;
				default:
					kdata = Hex.toArray(Hex.fromString(k));
			}
			// 3: get an output
			var txt:String = str;
			var data:ByteArray;
			var format:String = "hex";
			switch (format) {
				case "hex": data = Hex.toArray(txt); break;
				case "b64": data = Base64.decodeToByteArray(txt); break;
				default:
					data = Hex.toArray(Hex.fromString(txt));
			}
			// 1: get an algorithm..
			var name:String = "aes"+"-"+"ecb";
			
			var pad:IPad = new PKCS5;
			var mode:ICipher = Crypto.getCipher(name, kdata, pad);
			pad.setBlockSize(mode.getBlockSize());
			// if an IV is there, set it.
			if (mode is IVMode) {
				var ivmode:IVMode = mode as IVMode;
				
			}
			mode.decrypt(data);
			currentInput = data;
			var decryptReturn:String = displayInput();
			
			return decryptReturn;
		}
		
		private static function displayInput():String {
			
			if (currentInput==null) return "";
			var txt:String;
			var format:String = "text";
			switch (format) {
				case "hex": txt = Hex.fromArray(currentInput); break;
				case "b64": txt = Base64.encodeByteArray(currentInput); break;
				default:
					txt = Hex.toString(Hex.fromArray(currentInput)); break;
			}
			
			return txt;
		}
		private static function displayOutput():String {
			if (currentResult==null) return "";
			var txt:String;
			var format:String = "hex";
			switch (format) {
				case "hex": txt = Hex.fromArray(currentResult); break;
				case "b64": txt = Base64.encodeByteArray(currentResult); break;
				default:
					txt = Hex.toString(Hex.fromArray(currentResult)); break;
			}
			return txt;
		}
		
	}
	
}

